package com.service;

import maps.test.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.provider.MediaStore.Audio;

import com.PrayTime.PrayTime.EnumationPrayer;
import com.activity.MyMapActivity;
import com.commun.Param;
import com.commun.Utils;

public class LocalServiceTimer extends Service {

	private Handler serviceHandler = null;
	public static Boolean isServiceStarted = false;
	Thread thread;

	@Override
	public void onCreate() {

		serviceHandler = new Handler();
		thread = new Thread(new RunTask());
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

		serviceHandler.postDelayed(thread, 1000L);

		return START_STICKY;

	}

	@Override
	public void onDestroy() {

		serviceHandler.removeCallbacksAndMessages(thread);

	}

	@Override
	public boolean onUnbind(Intent intent) {
		serviceHandler.removeCallbacksAndMessages(thread);
		thread.stop();
		return super.onUnbind(intent);
	}

	@Override
	public IBinder onBind(Intent arg0) {
		return binder;
	}

	class RunTask implements Runnable {
		public void run() {
			if (LocalServiceTimer.isServiceStarted) {
				EnumationPrayer prayer = Utils.ChekPrayTime();

				if (prayer != EnumationPrayer.NO_ONE)

					Notifie(prayer);
				serviceHandler.postDelayed(this, 60 * 1000L);
			}
		}
	}

	private void Notifie(EnumationPrayer prayer) {
		int idRefNotification = 1;

		NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		notificationManager.cancel(idRefNotification);
		Notification notification = new Notification(R.drawable.icon,
				MyMapActivity.getmContext().getString(R.string.notification_pray_call), System.currentTimeMillis());
		
		 Intent notificationIntent = new Intent(
					MyMapActivity.getmContext(), MyMapActivity.class);
		 
		PendingIntent pendingIntent = PendingIntent
				.getActivity(MyMapActivity.getmContext(), 0,Param.OPEN_APPLICATION_ON_NOTIFICATION_CLICK ? notificationIntent
						: null , 0);

		if (Utils.notificationChoises == Utils.NotificationChoises.ACTIVER_SON
				|| Utils.notificationChoises == Utils.NotificationChoises.ACTIVER_SON_VIBRATION) {
			notification.defaults |= Notification.DEFAULT_SOUND;
			notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
			notification.audioStreamType = AudioManager.STREAM_NOTIFICATION;
		}

		if (Utils.notificationChoises == Utils.NotificationChoises.ACTIVER_VIBRATION
				|| Utils.notificationChoises == Utils.NotificationChoises.ACTIVER_SON_VIBRATION) {
			notification.vibrate = new long[] {0,200,100,200,100,200};
		}

		// set default config
		// notification.defaults = Notification.DEFAULT_SOUND |
		// Notification.DEFAULT_VIBRATE;
		String titreNotification = MyMapActivity.getmContext().getString(
				R.string.notificationSalat);

		String texteNotification = "";
		switch (prayer) {
		case FAJR:
			texteNotification = MyMapActivity.getmContext().getString(
					R.string.fajr);
			break;
		case DHOHR:
			texteNotification = MyMapActivity.getmContext().getString(
					R.string.dhohr);
			break;
		case ASR:
			texteNotification = MyMapActivity.getmContext().getString(
					R.string.asr);
			break;
		case MAGHREB:
			texteNotification = MyMapActivity.getmContext().getString(
					R.string.maghreb);
			break;
		case ISHA:
			texteNotification = MyMapActivity.getmContext().getString(
					R.string.isha);
			break;
		}

		
		notification.setLatestEventInfo(MyMapActivity.getmContext(),
				titreNotification, texteNotification,
				Param.OPEN_APPLICATION_ON_NOTIFICATION_CLICK ? pendingIntent
						: null);
		
		notification.contentIntent = pendingIntent;
		notificationManager.notify(idRefNotification++, notification);

	}

	public class AlarmServiceInterfaceImp extends AlarmServiceInterface.Stub {

		@Override
		public IBinder asBinder() {
			// TODO Auto-generated method stub
			return new MonServiceBinder();
		}

		public class MonServiceBinder extends Binder {
			public LocalServiceTimer getService() {
				return LocalServiceTimer.this;
			}
		}

		public void basicTypes(int anInt, long aLong, boolean aBoolean,
				float aFloat, double aDouble, String aString)
				throws RemoteException {

		}

		public int getPid() throws RemoteException {
			return 0;
		}
	}

	private final AlarmServiceInterfaceImp binder = new AlarmServiceInterfaceImp();

}

/*
 * Schedule the alarm! Intent intentAlarme = new
 * Intent(MyMapActivity.this,LocalServiceTimer.class); PendingIntent
 * intentAlarmeSuspend = PendingIntent.getBroadcast(MyMapActivity.this, 0,
 * intentAlarme, 0); long firstTime = SystemClock.elapsedRealtime();
 * AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
 * am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 5*1000,
 * intentAlarmeSuspend);
 */